package com.colorful.insuranceproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.colorful.insuranceproject.R;
import com.lovely3x.common.adapter.BaseViewHolder;
import com.lovely3x.common.utils.ViewUtils;
import com.lovely3x.common.viewcache.ViewPool;
import com.lovely3x.common.widgets.EnoughHeightGridView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import butterknife.Bind;

/**
 * 格子状的图片适配器
 * Created by lovely3x on 15-12-1.
 */
public class GridImgsAdapter extends com.lovely3x.common.adapter.ListAdapter<String> {

    public static final int CACHE_VIEW_TYPE = ViewPool.findUniquenessViewType();
    private final EnoughHeightGridView mGridView;
    private final ImageLoader mImageLoader;
    private final int mParentPosition;
    private ImageClickedListener mGridImageViewClickLitener;
    private int cols = 4;

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
    }

    public GridImgsAdapter(List<String> datas, int parentPosition, Context context, EnoughHeightGridView gridView, int cols) {
        super(datas, context);
        this.mGridView = gridView;
        mImageLoader = ImageLoader.getInstance();
        this.mParentPosition = parentPosition;
        this.cols = cols;
    }


    public GridImgsAdapter(List<String> datas, int parentPosition, Context context, EnoughHeightGridView gridView) {
        this(datas, parentPosition, context, gridView, 3);
    }

    @NonNull
    @Override
    public BaseViewHolder createViewHolder(final int position, ViewGroup parent) {
        ViewHolder viewHolder = null;
        View v = getLayoutInflater().inflate(R.layout.view_dynamic_img_item, parent, false);
        int width = ViewUtils.getWidth();
        int columnNum = mGridView.getNumColumns();
        width = width - mGridView.getPaddingRight() - mGridView.getPaddingLeft() - (columnNum % 2 == 0 ? columnNum / 2 : columnNum / 2 + 1);
        int itemSize = (int) (1.0 * width / cols);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(itemSize, itemSize);
        viewHolder = new ViewHolder(v);
        viewHolder.ivImg.setLayoutParams(lp);
        viewHolder.ivImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mViewClickedListener != null) {
                    mViewClickedListener.onViewClicked(position, v);
                }
                if (mGridImageViewClickLitener != null) {
                    mGridImageViewClickLitener.onImageClicked(mParentPosition, position, v);
                }
            }
        });
        return viewHolder;
    }

    @Override
    public void handleData(int position, @NonNull BaseViewHolder holder) {
        ViewHolder viewHolder = (ViewHolder) holder;
        String item = getDatas().get(position);
        if (!item.equals("")) {
            mImageLoader.displayImage(item, viewHolder.ivImg);
        } else {
            viewHolder.ivImg.setImageResource(R.drawable.short_load_failure);
        }
    }

    /**
     * 设置图片点击监听器
     *
     * @param listener 需要设置的图片点击监听器
     */
    public void setOnImgViewClickedListener(ImageClickedListener listener) {
        this.mGridImageViewClickLitener = listener;
    }

    /**
     * 图片点击监听器
     */
    public interface ImageClickedListener {
        /***
         * 当图片被点击后执行
         *
         * @param parentPosition 该视图所对应的的父位置
         * @param position       当前视图的位置
         * @param view           点击的视图对象
         */
        void onImageClicked(int parentPosition, int position, View view);
    }

    /**
     * the holder
     */
    public static class ViewHolder extends BaseViewHolder {

        @Bind(R.id.iv_view_dynamic_img_item)
        ImageView ivImg;

        /**
         * please don't modify the constructor
         *
         * @param rootView the root view
         */
        public ViewHolder(View rootView) {
            super(rootView);
        }
    }
}

